package es.andresimon.portfolio.domain;

import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;
import es.andresimon.portfolio.domain.Project;

@Configurable
@Component
public class ProjectDataOnDemand {

	private Random rnd = new java.security.SecureRandom();

	private List<Project> data;

	public Project getNewTransientProject(int index) {
        es.andresimon.portfolio.domain.Project obj = new es.andresimon.portfolio.domain.Project();
        java.lang.String column1 = "column1_" + index;
        if (column1.length() > 10000) {
            column1  = column1.substring(0, 10000);
        }
        obj.setColumn1(column1);
        java.lang.String column2 = "column2_" + index;
        if (column2.length() > 10000) {
            column2  = column2.substring(0, 10000);
        }
        obj.setColumn2(column2);
        obj.setFrontPicture(new Integer(index).longValue());
        java.lang.String introduction = "introduction_" + index;
        if (introduction.length() > 4000) {
            introduction  = introduction.substring(0, 4000);
        }
        obj.setIntroduction(introduction);
        obj.setProjectDate(new java.util.Date());
        obj.setProjectId(new Integer(index).longValue());
        java.lang.String title = "title_" + index;
        if (title.length() > 50) {
            title  = title.substring(0, 50);
        }
        obj.setTitle(title);
        java.lang.String url = "url_" + index;
        if (url.length() > 500) {
            url  = url.substring(0, 500);
        }
        obj.setUrl(url);
        obj.setUserRole(null);
        java.lang.String video = "video_" + index;
        if (video.length() > 1000) {
            video  = video.substring(0, 1000);
        }
        obj.setVideo(video);
        obj.setVisibility(Boolean.TRUE);
        obj.setWeight(new Integer(index));
        return obj;
    }

	public Project getSpecificProject(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Project obj = data.get(index);
        return Project.findProject(obj.getId());
    }

	public Project getRandomProject() {
        init();
        Project obj = data.get(rnd.nextInt(data.size()));
        return Project.findProject(obj.getId());
    }

	public boolean modifyProject(Project obj) {
        return false;
    }

	public void init() {
        data = es.andresimon.portfolio.domain.Project.findProjectEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Project' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new java.util.ArrayList<es.andresimon.portfolio.domain.Project>();
        for (int i = 0; i < 10; i++) {
            es.andresimon.portfolio.domain.Project obj = getNewTransientProject(i);
            obj.persist();
            obj.flush();
            data.add(obj);
        }
    }
}
